/*
 * 2005-12-28 lxm 创建。
 */
package com.hd123.rumba.organization;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.ReplicationMode;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.runtime.IAfterSaveNew;
import com.hd123.rumba.runtime.IBeforeSave;
import com.hd123.rumba.runtime.Manager;
import com.hd123.rumba.runtime.ObserverRegistry;
import com.hd123.rumba.runtime.OperateContext;
import com.hd123.rumba.util.ClassifiedHashtable;

/**
 * 组织角色 | 管理员 | 实现基类
 * <p>
 * 
 * 提供给以后组织角色子类的管理员实现类，具体方法从该类派生。
 * 
 * @author lxm
 * 
 */
public abstract class OrgRoleMgr extends Manager {

  public OrgRoleMgr() {
    super();
    this.registryClass("com.hd123.rumba.organization.IOrgRole",
        "com.hd123.rumba.organization.OrgRole");
  }

  public IOrgRole createOrgRole(IOrganization organization) throws BusinessException {
    IOrgRole orgRole = organization.getRole(this.getClass(), true);
    if (orgRole == null) {
      orgRole = createNewOrgRole(organization);
      orgRole.setDomain(organization.getDomain());
      orgRole.setOrganization(organization);
      orgRole.setState(COrgRole.VALUE_STATE_USING);
    }
    return orgRole;
  }

  /**
   * 新建一个组织角色。
   * 
   * @param organization
   *          组织角色所属组织。
   * @return
   * @throws BusinessException
   */
  public IOrgRole createNewOrgRole(IOrganization organization) throws BusinessException {
    // Do Nothing
    return null;
  }

  public void onBeforeSaveOrg(IOrgRole orgRole, OperateContext operate) throws BusinessException,
      HibernateException {
    orgRole.setLastModifier(operate.getOperator().getCodeName());
    orgRole.setLastModified(operate.getTime());
  }

  public void onAfterSaveOrg(IOrgRole orgRole, OperateContext operate) throws BusinessException,
      HibernateException {
    // Do Nothing
  }

  public void onDeleteOrg(IOrgRole orgRole, OperateContext operate) throws BusinessException,
      HibernateException {
    // Do Nothing
  }

  public void onUndeleteOrg(IOrgRole orgRole, OperateContext operate) throws BusinessException,
      HibernateException {
    // Do Nothing
  }

  public void onBeforeRemoveOrg(IOrgRole orgRole) throws BusinessException, HibernateException {
    // Do Nothing
  }

  public ClassifiedHashtable onBeforeAddOrgRole(IOrganization organization, IOrgRole orgRole)
      throws BusinessException, HibernateException {
    // Do Nothing
    return null;
  }

  public ClassifiedHashtable onBeforeRemoveOrgRole(IOrganization organization, IOrgRole orgRole)
      throws BusinessException, HibernateException {
    // Do Nothing
    return null;
  }

  public void changeOrgRole(IOrgRole orgRole, Integer toState, OperateContext operate, long oca)
      throws BusinessException, HibernateException {
    checkParameterNotNull(orgRole, "orgRole");
    checkParameterNotNull(toState, "toStatse");

    ObserverRegistry.notifyObservers(IBeforeSave.class, orgRole);

    orgRole.setState(toState);

    if (orgRole.getUuid() != null)
      getSession().replicate(orgRole, ReplicationMode.OVERWRITE);
    else {
      getSession().saveOrUpdate(orgRole);
      ObserverRegistry.notifyObservers(IAfterSaveNew.class, orgRole);
    }
  }

}
